let App = getApp()
let amap = require("../../assets/plugins/map/index")

let initReLocateLabel = '重新定位'
Page({
  data: {
    reLocateLabel: initReLocateLabel,
    poiList: [],
    showSearchList: false,
    searchKey: '',
    searchList: [],
    city: '',
    isChange: !0,
    addressShow: 1,
  },
  onLoad () {
    let position = wx.getStorageSync('position')
    this.setData({city: position.city, position})
    this.initPoiList()
  },
  reLocate (e) {
    this.initPoiList()
  },
  changeCity () {
    let { isChange, cityList } = this.data
    this.setData({isChange: !isChange, addressShow: !isChange})
    if (isChange) {
      App.HttpService.sellerCity().then(res => {
        const data = res.data

        this.setData({cityList: data.data})
      })
    }
  },
  onCityTap (e) {
    let {id, city} = e.currentTarget.dataset
    this.setData({city, isChange: !0, addressShow: 1})
  },
  onSearchInput (e) {
    let that = this
    let {value} = e.detail
    this.setData({
      searchKey: value,
      showSearchList: !!value
    })
    if (value) {
      let { isChange, cityList, city } = this.data
      amap.getInputtips(city, '', value, true).then(res =>{
        let tips = res.tips
        if (tips.length > 0) {
          tips.forEach((item, index)=> {
            let length = item.location
            if (!typeof (length)){
              if (item.location.length == 0) {
                tips.splice(index, 1)
              }
            }
          })
          this.setData({
            searchList: res.tips,
            isChange: !0,
            addressShow: 1
          })
        }
      })
    }
  },
  clearSearchKey (e) {
    this.setData({
      searchKey: '',
      showSearchList: false
    })
  },
  onSearchItemTap (e) {
    let { keywords, location } = e.currentTarget.dataset
    if (location) {
      amap.getPoiAround(keywords, location).then(res => {
        let poiList = res.poisData
        let { cityname } = poiList[0]
        wx.setStorageSync('position', {
          location,
          city: cityname
        })
        this.callback(keywords)
      }).catch(e => {
      })
    }
  },
  onPoiItemTap (e) {
    let { location, name } = e.currentTarget.dataset
    let { position } = this.data
    wx.setStorageSync('position', {
      location,
      city: position.city
    })
    this.callback(name)
  },
  initPoiList () {
    this.setData({
      reLocateLabel: '定位中...'
    })
    amap.getRegeo ().then(res =>{
      let data = res[0]
      let address = data.regeocodeData.pois
      this.setData({
        poiList: address,
        reLocateLabel: initReLocateLabel
      })
    })
  },
  callback (keywords) {
    var pages = getCurrentPages()
    var prePage = pages[pages.length - 2]
    prePage.onPullDownRefresh()
    prePage.setData({currentAddress: keywords})

    let city = this.data.searchList;

    city.forEach((item,index) => {
      if(item.name == keywords){
        let lnglat = item.location.split(",");
        this.getAgent(lnglat[0],lnglat[1])
      }
    })
    wx.navigateBack()
  },
  getAgent(lng, lat) {
    App.HttpService.sellerCountry({ lng: lng, lat: lat }).then(res => {
      let data = res.data
      let agent = {}
      if (data.code == 200) {
        agent.id = data.data.sellerId
        this.setData({ agent });
      } else {
        agent.id = ''
      }
      wx.setStorageSync('agent', agent);
    })
  }
})